import { Post, Body, Get, Param, Patch, Delete, Query } from '@nestjs/common'
import { BaseService } from './base.service'
import { ApiOperation } from '@nestjs/swagger'

/**
 * 定义的公共控制类
 */
export class BaseController<T, D extends Object> {
  constructor(private readonly baseService: BaseService<T, D>, private readonly modelName?: string) {
    this.modelName = modelName
  }

  @ApiOperation({ summary: `创建接口`, description: '创建记录' })
  @Post()
  async create(@Body() dto: D): Promise<string | null> {
    return await this.baseService.create(dto)
  }

  @ApiOperation({ summary: `获取列表`, description: '获取全部记录' })
  @Get()
  async findAll(@Query() query: T): Promise<T[]> {
    return await this.baseService.findAll(query)
  }

  @ApiOperation({ summary: `获取详情`, description: '获取单个记录' })
  @Get('one/:id')
  async findOne(@Param('id') id: string): Promise<T | null> {
    return await this.baseService.findOne(id)
  }

  @ApiOperation({ summary: `获取分页列表`, description: '获取分页后记录' })
  @Get('page')
  async findPage(@Query() query: any): Promise<Result.pageData<T>> {
    return await this.baseService.findPage(query)
  }

  @ApiOperation({ summary: `修改接口`, description: '修改记录' })
  @Patch(':id')
  async update(@Param('id') id: string, @Body() dto: D): Promise<string | null> {
    return await this.baseService.update(id, dto)
  }

  @ApiOperation({ summary: `删除接口`, description: '删除记录' })
  @Delete(':id')
  async remove(@Param('id') id: string): Promise<string | null> {
    return await this.baseService.remove(id)
  }

  @ApiOperation({ summary: `批量删除接口`, description: '批量删除记录' })
  @Delete('many/:ids')
  async removeMany(@Param('ids') ids: string): Promise<string | null> {
    return await this.baseService.removeMany(ids)
  }
}
